package net.dgg.rz.api.production.controller;

import io.swagger.annotations.Api;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.api.production.service.ApiProductionService;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.production.entity.PageList;
import net.dgg.rz.production.entity.ProductNodeOperating;
import net.dgg.rz.production.entity.dto.ScOrderAndTaskDTO;
import net.dgg.rz.production.entity.dto.TaskOperateRecordDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>@Title 生产部分对外接口controller</p>
 * <p>@Description 办理中相关</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author maoquanyi</p>
 * <p>@date 2018年08月13日</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@Controller
@RequestMapping("/api/system/portal/task")
@Api(description = "生产给IBOSS提供的接口")
public class ApiOutOrderController extends DggBaseController {

    @Resource
    private ApiProductionService apiProductionService;

    /**
     * @Description 返回生产信息及节点信息给订单中心
     * @author 胡隆再
     * @date 2020/7/29 14:50
     * @param params
     * @return net.dgg.framework.tac.utils.bean.DggRestResponse
     */
    @PostMapping("/v1/sc_order_task_by_sc_order_id.do")
    @ResponseBody
    public DggRestResponse scOrderTaskByScOrderId(@RequestBody Map params) {
        Integer start = Integer.parseInt(String.valueOf(params.get("start")));
        Integer limit = Integer.parseInt(String.valueOf(params.get("limit")));
        List<Long> scOrderIds = (List<Long>) params.get("scOrderIds");
        //参数判断
        if (StringUtils.isEmpty(start)) {
            return buildResponse(6002, "参数不正确", start);
        }
        if (StringUtils.isEmpty(limit)) {
            return buildResponse(6002, "参数不正确", limit);
        }
        if (StringUtils.isEmpty(scOrderIds) || scOrderIds.size() < 1) {
            return buildResponse(6002, "参数不正确", scOrderIds);
        }
        try {
            //查询生产单相关信息
            List<ScOrderAndTaskDTO> scOrderAndTaskDTOList = apiProductionService.getScOrderAndTask(start, limit, scOrderIds);
            return buildResponse(200, "操作成功", scOrderAndTaskDTOList);
        } catch (DggBaseException e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001, "系统异常", e);
        } catch (Exception e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001, "系统异常", e);
        }
    }
    /**
     * @Description 返回生产信息及节点信息给订单中心
     * @author 胡隆再
     * @date 2020/7/29 14:50
     * @param params
     * @return net.dgg.framework.tac.utils.bean.DggRestResponse
     */
    @PostMapping("/v1/product_node_operating.do")
    @ResponseBody
    public DggRestResponse getProductNodeOperating(@RequestBody Map params) {
        String scOrderId =  String.valueOf(params.get("scOrderId"));
        if (StringUtils.isEmpty(scOrderId)) {
            return buildResponse(6002, "生产单号不能为空", scOrderId);
        }
        try {
            //查询生产单相关信息
            PageList<TaskOperateRecordDTO> scOrderAndTaskDTOList = apiProductionService.getProductNodeOperating(params);
            return buildResponse(200, "操作成功", scOrderAndTaskDTOList);
        } catch (DggBaseException e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001, "系统异常", e);
        } catch (Exception e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001, "系统异常", e);
        }
    }
}
